package com.cctc.configuration;

import java.beans.PropertyVetoException;
import java.util.Properties;

import org.hibernate.dialect.MySQLDialect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;

import com.cctc.fw.util.PropertiesReaderUtil;
import com.mchange.v2.c3p0.ComboPooledDataSource;

@Configuration
public class FwConfiguration
{
    // private Class<?>[] domainClass = new Class[] { User.class, Authoritie.class, Resource.class, Group.class };
    private Class<?>[] domainClass = new Class[] {};
    
    public FwConfiguration()
    {
        PropertiesReaderUtil.init("configure.properties");
    }
    
    @Bean
    public AnnotationSessionFactoryBean sessionFactoryBean()
    {
        
        Properties pro = PropertiesReaderUtil.getPro();
        
        Properties properties = new Properties();
        //显示官方的语言
        properties.put("hibernate.dialect", MySQLDialect.class.getName());
        //显示打印的语句
        properties.put("hibernate.show_sql", pro.getProperty("show_sql"));
        //格式化语句
        properties.put("hibernate.format_sql", pro.getProperty("format_sql"));
        //数据库的操作
        properties.put("hibernate.hbm2ddl.auto", pro.getProperty("hbm2ddl.auto"));
        
        properties.put("hibernate.use_sql_comments", pro.getProperty("use_sql_comments"));
        //Hibernate将把注释插入到所有生成的SQL语句中，以提示生成的SQL语句在做什么
        properties.put("connection.autocommit", pro.getProperty("connection.autocommit"));
        properties.put("current_session_context_class", pro.getProperty("current_session_context_class"));
        
        properties.put("hibernate.jdbc.fetch_size", pro.getProperty("jdbc.fetch_size"));
        properties.put("hibernate.jdbc.batch_size", pro.getProperty("jdbc.batch_size"));
        properties.put("hibernate.temp.use_jdbc_metadata_defaults", pro.getProperty("use_jdbc_metadata_defaults"));
        
        //配置c3p0连接池
        properties.put("hibernate.connection.provider_class", pro.getProperty("connection.provider_class"));
        properties.put("hibernate.c3p0.max_size", pro.getProperty("c3p0.max_size"));
        properties.put("hibernate.c3p0.min_size", pro.getProperty("c3p0.min_size"));
        properties.put("hibernate.c3p0.timeout", pro.getProperty("c3p0.timeout"));
        properties.put("hibernate.c3p0.max_statements", pro.getProperty("c3p0.max_statements"));
        properties.put("hibernate.c3p0.idle_test_period", pro.getProperty("c3p0.idle_test_period"));
        properties.put("hibernate.c3p0.acquire_increment", pro.getProperty("c3p0.acquire_increment"));
        properties.put("hibernate.c3p0.validate", pro.getProperty("c3p0.validate"));
        properties.put("hibernate.autoReconnect", pro.getProperty("autoReconnect"));
        properties.put("hibernate.connection.release_mode", pro.getProperty("connection.release_mode"));
        properties.put("hibernate.generate_statistics", pro.getProperty("generate_statistics"));
        properties
                .put("com.mchange.v2.c3p0.management.ManagementCoordinator", pro.getProperty("managementCoordinator"));
        
        //cache
        properties.put("hibernate.cache.use_second_level_cache", pro.getProperty("cache.use_second_level_cache"));
        properties.put("hibernate.cache.provider_class", pro.getProperty("cache.provider_class"));
        properties.put("hibernate.cache.use_query_cache", pro.getProperty("cache.use_query_cache"));
        
        AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
        
        bean.setAnnotatedClasses(domainClass);
        bean.setHibernateProperties(properties);
        
        bean.setDataSource(dataSource());
        bean.setSchemaUpdate(true);
        
        return bean;
    }
    
    @Bean
    public ComboPooledDataSource dataSource()
    {
        Properties pro = PropertiesReaderUtil.getPro();
        
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try
        {
            dataSource.setDriverClass(pro.getProperty("driverClassName"));
            dataSource.setJdbcUrl(pro.getProperty("url"));
            dataSource.setUser(pro.getProperty("username"));
            dataSource.setPassword(pro.getProperty("password"));
            dataSource.setInitialPoolSize(Integer.parseInt(pro.getProperty("initialpoolsize")));
            dataSource.setMaxPoolSize(Integer.parseInt(pro.getProperty("maxpoolsize")));
        }
        catch (PropertyVetoException e)
        {
            e.printStackTrace();
        }
        return dataSource;
    }
    
    @Bean
    public HibernateTemplate hibernateTemplate()
    {
        return new HibernateTemplate(sessionFactoryBean().getObject());
    }
    
    @Bean
    public HibernateTransactionManager transactionManager()
    {
        return new HibernateTransactionManager(sessionFactoryBean().getObject());
    }
}
